package net.hancl.modules.sys.service.impl;

import net.hancl.common.bean.page.Query;
import net.hancl.common.bean.page.QueryResult;
import net.hancl.modules.sys.bean.UserBean;
import net.hancl.modules.sys.dao.SysLogMapper;
import net.hancl.modules.sys.service.ILogService;
import net.hancl.modules.sys.entity.SysLog;
import net.hancl.modules.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LogService implements ILogService {

    @Autowired
    SysLogMapper sysLogMapper;

    @Autowired
    private IUserService userService;

    public QueryResult<SysLog> getList(Integer page, Integer pagesize, SysLog log) {
        Query query = new Query();
        query.setBean(log);
        query.setPageIndex(page);
        query.setPageSize(pagesize);
        // 组合分页信息
        QueryResult<SysLog> queryResult = new QueryResult<SysLog>();
        Long count = sysLogMapper.getCount(query);
        List<SysLog> list = sysLogMapper.queryPages(query);
        if(list!=null&&list.size()>0)
        {
            for(SysLog sysLog:list){
                if(sysLog.getCreateById()!=null){
                    UserBean user=userService.get(sysLog.getCreateById());
                    sysLog.setCreateByName(user.getUserName());
                }
            }
        }
        // 总页数 和 取多少条
        queryResult.setCurrentPage(page);
        queryResult.setPages(count, pagesize);
        queryResult.setItems(list);
        return queryResult;
    }

    public int add(SysLog log) {
        if(log==null){
            return 0;
        }
//		log.setCreateTime(new Date());
        sysLogMapper.insertSelective(log);
        return 0;
    }
}
